package com.example.metaactivity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.TimePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;

public class EditFocusActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_focus);

        EditText phone = findViewById(R.id.edit_phone);

        EditText passWord = findViewById(R.id.et_password);
        passWord.setOnFocusChangeListener(
                new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        if (hasFocus){
                            CharSequence phoneNo = phone.getText();
                            if (TextUtils.isEmpty(phoneNo) || phoneNo.length() != 11){
                                phone.requestFocus();
                                Toast.makeText(EditFocusActivity.this, "电话号码长度错误", Toast.LENGTH_SHORT).show();
                            }else{
                                InputMethodManager inputMethodManager = (InputMethodManager) EditFocusActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
                                inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
                            }
                        }
                    }
                }
        );
        phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                phone.removeTextChangedListener(this);
            }
        });

        Button openTimePicker = findViewById(R.id.open_timepicker);
        Calendar calendar = Calendar.getInstance();
        openTimePicker.setOnClickListener(
                item -> {
                   new TimePickerDialog(EditFocusActivity.this, new TimePickerDialog.OnTimeSetListener() {
                       @Override
                       public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                           Toast.makeText(EditFocusActivity.this, "选中了" + hourOfDay +"时" + minute + "分", Toast.LENGTH_SHORT).show();
                       }
                   }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true).show();
                }
        );
    }
}